package cn.com.jrj.base.util;

import java.text.DecimalFormat;

public class Convert {
	
	/**
	 * 取字符串的前toCount个字符,如果遇到汉字截取半个的,按一个算.
	 * @param str   被处理字符串
	 * @param toCount 截取长度
	 * @param isellipsis 是否显示省略号
	 * @return
	 */
	public static String substring(String str, int toCount,String isellipsis)
	{
		  if(toCount==0) return str;
	      int reInt = 0;
	      String reStr = "";
	      if (str == null)
	        return "";
	      char[] tempChar = str.toCharArray();
	      for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) 
	      {
	        String s1 = str.valueOf(tempChar[kk]);
	        byte[] b = s1.getBytes();
	        reInt += b.length;
	        reStr += tempChar[kk];
	      }
	      int cl = str.getBytes().length;
	      int ct = reStr.getBytes().length;
	      if (cl>ct && isellipsis.equals("0")){
	        reStr += ".";
	      }
	      return reStr.trim();
	      
	  	/**
	  	 * 取字符串的前toCount个字符,如果遇到汉字截取半个的,按一个算.
	  	 * @param str   被处理字符串
	  	 * @param toCount 截取长度
	  	 * @param isellipsis 是否显示省略号
	  	 * @return
	  	 */
	}public static String substringForThree(String str, int toCount,String isellipsis)
	{
		  if(toCount==0) return str;
	      int reInt = 0;
	      String reStr = "";
	      if (str == null)
	        return "";
	      char[] tempChar = str.toCharArray();
	      for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) 
	      {
	        String s1 = str.valueOf(tempChar[kk]);
	        byte[] b = s1.getBytes();
	        reInt += b.length;
	        reStr += tempChar[kk];
	      }
	      int cl = str.getBytes().length;
	      int ct = reStr.getBytes().length;
	      if (cl>ct && isellipsis.equals("0")){
	        reStr += "...";
	      }
	      return reStr.trim();
	}
	public static String getDoubleFormat(double d,String f){
		if(f==null) f="0.00";
		DecimalFormat df = new DecimalFormat(f);
		return df.format(d);
	}
}
